package com.szosen.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 保存数据到缓存SharedPreferences
 */
public class Cache {

	public static final String KRLBP = "KrlBp";
	public static final String PC300 = "PC300";
	public static final String BENECHECK = "BeneCheck";// 百捷血糖设备
	public static final String GMPUA = "GmpUa";// 尿液分析仪
	public static final String MO = "mo";// 体质分析仪

	private static final String USER_ID = "user_id";
	public static final String ITEM = "ITEM";
	public static final String BP = "bp";
	public static final String TEMP = "temp";
	public static final String BO = "bo";
	public static final String GLU = "glu";
	public static final String GLU_SANNUO = "glu_sannuo";
	public static final String UA = "ua";
	public static final String CHOL = "chol";
	public static final String URINE = "urine";
	private static final String GROUP_ID = "group_id";
	private static final String CONTAC_NAMES = "names";
	private static final String CONTACT = "Contact";
	public static final String NICKNAME = "nickname";
	public static final String USERNAME = "username";
	public static final String PASSWORD = "password";
	public static final String SEX = "sex";
	public static final String PHONE = "phone";
	public static final String ADDRESS = "address";
	public static final String TOKEN = "token";
	public static final String IDREADER = "idreader";
	public static final String DOCGUID = "docguid";
	public static final String VERSION="version";
	public static final String XUEZHI="xuezhi";
	Context context;
	private SharedPreferences sharedPrefrences;
	private Editor editor;

	public Cache(Context context) {
		this.context = context;
		sharedPrefrences = context.getSharedPreferences("padhealth",
				Context.MODE_PRIVATE);
		editor = sharedPrefrences.edit();
	}

	//
	// /**
	// * 保存当前用户id
	// *
	// * @param idCard
	// */
	// public void saveUserId(String idCard) {
	// editor.putString(USER_ID, idCard);
	// editor.commit();// 提交
	// }
	//
	// /**
	// * 获取当前用户id
	// *
	// * @return
	// */
	// public String getUserId() {
	// return sharedPrefrences.getString(USER_ID,
	// null);
	// }

	/**
	 * 保存设备地址
	 *
	 * @param device
	 * @param address
	 */
	public void saveDeviceAddress(String device, String address) {
		editor.putString(device, address);
		editor.commit();// 提交
	}

	/**
	 * 获取设备的地址
	 *
	 * @param device
	 * @return
	 */
	public String getDeviceAddress(String device) {
		return sharedPrefrences.getString(device, null);
	}




	/***
	 * 保存当前用户信息
	 *
	 * @param userInfo
	 */
	public void saveUserInfo(JSONObject userInfo) {
		editor.putString("userInfo", userInfo.toString());
		editor.commit();// 提交
	}

	/***
	 * 保存当前用户信息
	 *
	 * @param userInfo
	 */
	public JSONObject getUserInfo() {
		JSONObject json = new JSONObject();
		try {
			json = new JSONObject(sharedPrefrences.getString("userInfo", "{}"));
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return json;
	}

	/***
	 * 获取当前用户名字
	 *
	 * @return
	 */
	public String getUserName() {
		String userName = "";
		JSONObject json = getUserInfo();
		try {
			userName = json.getString("name");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return userName;

	}

	/***
	 * 保存机构号
	 *
	 * @param count
	 */
	public void saveGroupId(String count) {
		editor.putString(GROUP_ID, count);
		editor.commit();// 提交
	}

	/**
	 * 获取当前用户id
	 *
	 * @return
	 */
	public String getGroupId() {
		return sharedPrefrences.getString(GROUP_ID, null);
	}

	/***
	 * 保存所有用户的名字
	 *
	 * @param count
	 */
	public boolean saveContactNames(String names) {
		editor.putString(CONTAC_NAMES, names);
		return editor.commit();// 提交
	}

	public String getContactNames() {
		return sharedPrefrences.getString(CONTAC_NAMES, null);
	}

	/***
	 * 保存联系人姓名和电话
	 *
	 * @param name
	 * @param phone
	 */
	public boolean saveContact(String name, String phone) {
		editor.putString(CONTACT + name, phone);
		return editor.commit();// 提交
	}

	/**
	 * 根据姓名获取电话
	 *
	 * @param name
	 * @return
	 */
	public String getContact(String name) {
		return sharedPrefrences.getString(CONTACT + name, null);
	}

	public void saveVersion(int i){
		editor.putInt(VERSION, i);
	}

	public int getVersion(){
		return sharedPrefrences.getInt(VERSION, 0);
	}
	final static String DEVICE_NAME1="CVR-100B";
//	final static String DEVICE_NAME2 = "IDCReader";
//	final static String DEVICE_NAME3 = "COM2";
//	final static String DEVICE_NAME4 = "BOLUTEK";
//	final static String[] DEVICE_NAME = new String[]{DEVICE_NAME1,DEVICE_NAME2,DEVICE_NAME3,DEVICE_NAME4};
	public void saveDeviceAddressIDCard(String address) {
		//for (String deviceIDCard:DEVICE_NAME) {
		//	editor.putString(deviceIDCard, address);
		//}
		editor.putString(DEVICE_NAME1, address);
		editor.commit();// 提交
	}
	public String getDeviceAddressIDCard(){
//		String device = null;
//		for (String deviceIDCard:DEVICE_NAME) {
//			device = sharedPrefrences.getString(deviceIDCard, null);
//		}
		return sharedPrefrences.getString(DEVICE_NAME1, null);
	};
	final static String HC_06="HC-06";

	public void saveDeviceAddressgetHc06(String address) {
		editor.putString(HC_06, address);
		editor.commit();// 提交
	}

	/**
	 * 获取设备的地址
	 *
	 * @param device
	 * @return
	 */
	public String getDeviceAddressHC_06() {
		return sharedPrefrences.getString(HC_06, null);
	}
}
